package com.sgcc.lms.openstack.service.impl;

import com.sgcc.lms.openstack.exception.NullTokenException;
import com.sgcc.lms.openstack.model.po.Instance;
import com.sgcc.lms.openstack.service.NovaService;
import com.sgcc.lms.openstack.util.OpenStackClientFactory;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-23 16:47
 * @Description:
 */
@Service
public class NovaServiceImpl implements NovaService {

    /**
     * @Author 李孟帅
     * @Description 列出实例类型
     * @Date 2020/7/23
     **/
    @Override
    public List<? extends Flavor> flavors(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Flavor> flavors = openStackClient.compute().flavors().list();
        return flavors;
    }

    /**
     * @Author 李孟帅
     * @Description 获取实例类型
     * @Date 2020/7/23
     **/
    @Override
    public Flavor getFlavor(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Flavor flavor = openStackClient.compute().flavors().get(id);
        return flavor;
    }

    /**
     * @Author 李孟帅
     * @Description 删除实例类型
     * @Date 2020/7/23
     **/
    @Override
    public ActionResponse deleteFlavor(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ActionResponse response = openStackClient.compute().flavors().delete(id);
        return response;
    }

    /**
     * @Author 李孟帅
     * @Description 创建实例类型
     * @Date 2020/7/23
     **/
    @Override
    public Flavor createFlavor(String tokenId, Flavor flavor) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Flavor flavor1 = openStackClient.compute().flavors().create(flavor);
        return flavor1;
    }

    /**
     * @Author 李孟帅
     * @Description 获取特定项目的配额集
     * @Date 2020/7/23
     **/
    @Override
    public QuotaSet getQuotaSet(String tokenId, String projectId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        QuotaSet quotaSet = openStackClient.compute().quotaSets().get(projectId);
        return quotaSet;
    }


    /**
     * @Author 李孟帅
     * @Description 更新特定项目的配额集
     * @Date 2020/7/23
     **/
    @Override
    public QuotaSet updateQuotaSet(String tokenId, String projectId, QuotaSetUpdate quotaSetUpdate) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        QuotaSet quotaSet = openStackClient.compute().quotaSets().updateForTenant(projectId, quotaSetUpdate);
        return quotaSet;
    }

    /**
     * @Author 李孟帅
     * @Description 列出密钥对
     * @Date 2020/7/23
     **/
    @Override
    public List<? extends Keypair> keypairs(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Keypair> keypairs = openStackClient.compute().keypairs().list();
        return keypairs;
    }

    /**
     * @Author 李孟帅
     * @Description 获取密钥对
     * @Date 2020/7/23
     **/
    @Override
    public Keypair getKeypair(String tokenId, String name) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Keypair keypair = openStackClient.compute().keypairs().get(name);
        return keypair;
    }

    /**
     * @Author 李孟帅
     * @Description 删除密钥对
     * @Date 2020/7/23
     **/
    @Override
    public ActionResponse deleteKeypair(String tokenId, String name) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ActionResponse response = openStackClient.compute().keypairs().delete(name);
        return response;
    }

    /**
     * @Author 李孟帅
     * @Description 创建密钥对
     * @Date 2020/7/23
     **/
    @Override
    public Keypair createKeypair(String tokenId, String name, String publicKey) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Keypair keypair1 = openStackClient.compute().keypairs().create(name, publicKey);
        return keypair1;
    }

    /**
     * @Author 李孟帅
     * @Description 列出实例
     * @Date 2020/7/24
     **/
    @Override
    public List<? extends Server> servers(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Server> servers = openStackClient.compute().servers().list(false);
        return servers;
    }

    /**
     * @Author 李孟帅
     * @Description 获取实例
     * @Date 2020/7/24
     **/
    @Override
    public Server getServer(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Server server = openStackClient.compute().servers().get(id);
        return server;
    }

    /**
     * @Author 李孟帅
     * @Description 创建实例
     * @Date 2020/7/24
     **/
    @Override
    public Server createServer(String tokenId, Instance instance) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ServerCreate build = openStackClient.compute().servers().serverBuilder()
                .name(instance.getName())
                .image(instance.getImageId())
                .flavor(instance.getFlavorId())
                .keypairName(instance.getKeyName())
                .addSecurityGroup(instance.getSecurityGroupName())
                .addNetworkPort(instance.getNetworkPortId())
                .build();
        Server server = openStackClient.compute().servers().boot(build);
        return server;
    }

    /**
     * @Author 李孟帅
     * @Description 删除实例
     * @Date 2020/7/24
     **/
    @Override
    public ActionResponse deleteServer(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ActionResponse response = openStackClient.compute().servers().delete(id);
        return response;
    }

    /**
     * @Author 李孟帅
     * @Description 获取实例VNC控制台
     * @Date 2020/7/24
     **/
    @Override
    public VNCConsole getConsole(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        VNCConsole vncConsole = openStackClient.compute().servers().getVNCConsole(id, VNCConsole.Type.NOVNC);
        return vncConsole;
    }
}
